package search;
//二分插入
public class leetcode35 {
    public static void main(String[] args) {
        int[] nums = {1,3,5,6};
        int t = 4;
        System.out.println(searchInsert(nums, t));
    }

    public static int searchInsert(int[] nums, int target) {
        int left = 0;
        int right = nums.length - 1;

        while(left <= right){
            int middle = (left + right)/2;
            if(nums[middle] == target) return middle;
            else if(nums[middle] > target){
                right = middle - 1;
            }
            else{
                left = middle + 1;
            }
        }

        return right + 1;
    }
}
